import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '@/views/home/HomeView.vue'
import Label from '@/components/Label.vue'
import userRoutes from './modules/user.js'
import store from '@/store'
import { Toast } from 'vant'

Vue.use(VueRouter)


const routes = [{
		//首页
		path: '/',
		name: 'home',
		component: HomeView,
		meta:{
			root:true,
			cache:true
		}
	},
	{
		//发布
		path: '/publish',
		name: 'PublishView',
		component: () => import('@/views/PublishView.vue'),
		meta:{
			root:true,
			cache:true
		}
	},
	{
		//我的
		path: '/fertile',
		name: 'FertileView',
		component: () => import('@/views/FertileView.vue'),
		meta:{
			root:true,
			cache:true
		}
	},
	{
		//搜索
		path: '/search',
		name: 'SearchView',
		component: () => import('@/views/search/SearchView.vue'),
	},
	{
		//搜索列表
		path: '/searchlist',
		name: 'SearchView',
		component: () => import('@/views/search/SearchListView.vue'),
	},
	{
		//详情
		path: '/detail',
		name: 'DetailView',
		component: () => import('@/views/detail/DetailView.vue'),
	},
	{
		//个人
		path: '/userinfo',
		name: 'UserinfoView',
		component: () => import('@/views/userinfo/UserinfoView.vue'),
	},
	
	
	...userRoutes
]

const router = new VueRouter({
	routes
})

router.beforeEach((to,from,next)=>{
	if(to.meta.auth){
		let isLogin = store.state.UESR.isLogin;
		if(isLogin){
			next()
		}else{
			Toast('请登录')
			setTimeout(v=>{
				router.push('/login')
			},300)
		}
	}else{
		next()
	}
})

export default router
